package cn.csc.module.product.controller.admin.spu;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.framework.security.core.annotations.PreAuthenticated;
import cn.csc.module.product.controller.admin.skuattrjson.vo.SkuAttrJsonRespV2VO;
import cn.csc.module.product.controller.admin.skuattrjson.vo.SkuAttrJsonSimpleReqVO;
import cn.csc.module.product.controller.admin.spu.vo.*;
import cn.csc.module.product.convert.sku.SkuConvert;
import cn.csc.module.product.convert.skuattrjson.SkuAttrJsonConvert;
import cn.csc.module.product.convert.spu.SpuConvert;
import cn.csc.module.product.dal.dataobject.sku.SkuDO;
import cn.csc.module.product.dal.dataobject.skuattrjson.SkuAttrJsonDO;
import cn.csc.module.product.dal.dataobject.spu.SpuDO;
import cn.csc.module.product.service.sku.SkuService;
import cn.csc.module.product.service.skuattrjson.SkuAttrJsonService;
import cn.csc.module.product.service.spu.SpuV2Service;
import cn.csc.module.product.validated.Verify1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

/**
 * @author 云隐
 * @date 2024/11/22
 **/
@Slf4j
@Api(tags = "管理后台 - spu信息")
@RestController
@RequestMapping("/product/spu/v2")
public class SpuV2Controller {

    @Resource
    private SpuV2Service spuV2Service;
    @Resource
    private SkuService skuService;
    @Resource
    private SkuAttrJsonService skuAttrJsonService;

    @PostMapping("/create")
    @ApiOperation("创建spu信息")
    @PreAuthorize("@ss.hasPermission('product:spuv2:create')")
    public CommonResult<BigInteger> createSpu(@Validated @RequestBody SpuCreateDataReqV2VO createDataReqVO) {
        return success(spuV2Service.createSpu(createDataReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新spu信息")
    @PreAuthorize("@ss.hasPermission('product:spuv2:update')")
    public CommonResult<Boolean> updateSpu(@Validated(Verify1.class) @RequestBody SpuCreateDataReqV2VO createDataReqVO) {
        spuV2Service.updateSpu(createDataReqVO);
        return success(true);
    }


    @PostMapping("/examine")
    @ApiOperation("商品上下架")
    @PreAuthorize("@ss.hasPermission('product:spuv2:examine')")
    public CommonResult<Boolean> examine(@RequestBody SpuUpdateReqV2VO updateReqVO) {
        SpuUpdateReqV2VO reqVO = new SpuUpdateReqV2VO();
        reqVO.setId(updateReqVO.getId());
        reqVO.setVerifyStatus(updateReqVO.getVerifyStatus());
        spuV2Service.updateShelf(reqVO);
        return success(true);
    }

    @PostMapping("/shelf")
    @Operation(summary = "商品上下架", description = "商品上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "商品ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "verifyStatus", required = true, value = "审核状态", example = "1", dataTypeClass = Integer.class)
    })
    @PreAuthenticated
    public CommonResult<Boolean> shelf(@Validated @RequestBody SpuShelfReqV2VO spuShelfReqVO) {
        SpuUpdateReqV2VO reqVO = new SpuUpdateReqV2VO();
        reqVO.setId(spuShelfReqVO.getId());
        reqVO.setVerifyStatus(spuShelfReqVO.getVerifyStatus());
        spuV2Service.onShelf(reqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @ApiOperation("删除spu信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spuv2:delete')")
    public CommonResult<Boolean> deleteSpu(@RequestParam("id") BigInteger id) {
        spuV2Service.deleteSpu(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品详情信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spuv2:query')")
    public CommonResult<SpuDataRespV2VO> getSpu(@RequestParam("id") BigInteger id) {
        SpuDataRespV2VO spu = spuV2Service.getSpuById(id);
        return success(spu);
    }

    @GetMapping("/list")
    @ApiOperation("获得spu信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spuv2:query')")
    public CommonResult<List<SpuRespVO>> getSpuList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuDO> list = spuV2Service.getSpuList(ids);
        return success(SpuConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得spu信息分页")
    @PreAuthorize("@ss.hasPermission('product:spuv2:query')")
    public CommonResult<PageResult<SpuRespV2VO>> getSpuPage(SpuPageReqV2VO pageVO) {
        PageResult<SpuDO> pageResult = spuV2Service.selectPageByOnLineTimeDesc(pageVO);
        PageResult<SpuRespV2VO> result = SpuConvert.INSTANCE.convertV2Page(pageResult);
        result.getList().forEach(s->{
             List<SkuDO> skuList = skuService.getSkuList(s.getId());
            s.setSkuList(SkuConvert.INSTANCE.convertList06(skuList));
        });
        return success(result);
    }

    @GetMapping("/pageExamine")
    @ApiOperation("获得spu信息分页")
    @PreAuthorize("@ss.hasPermission('product:spuv2:query')")
    public CommonResult<PageResult<SpuRespV2VO>> pageExamine(SpuPageReqV2VO pageVO) {
        PageResult<SpuDO> pageResult = spuV2Service.selectPageExamine(pageVO);
        PageResult<SpuRespV2VO> result = SpuConvert.INSTANCE.convertV2Page(pageResult);
        result.getList().forEach(s->{
            List<SkuDO> skuList = skuService.getSkuList(s.getId());
            s.setSkuList(SkuConvert.INSTANCE.convertList06(skuList));
        });
        return success(result);
    }

    @GetMapping("/getSkuById")
    @ApiOperation("获得商品sku属性关联JSON信息分页")
    @PreAuthorize("@ss.hasPermission('product:spuv2:query')")
    public CommonResult<SkuAttrJsonRespV2VO> getSkuById(@RequestParam("spuId") BigInteger spuId) {
        SkuAttrJsonDO skuAttrJsonDO = skuAttrJsonService.getSkuAttrJsonBySpuId(spuId);
        SkuAttrJsonRespV2VO respV2VO =SkuAttrJsonConvert.INSTANCE.convert1(skuAttrJsonDO);
        SpuDO spuDO = spuV2Service.getSpu(spuId);
        respV2VO.setProductName(spuDO.getName());
        respV2VO.setMainImgUrl(spuDO.getMainImgUrl());
        return success(respV2VO);
    }

    @PostMapping("/updateSkuById")
    @ApiOperation("单独修改sku属性")
    @PreAuthorize("@ss.hasPermission('product:spuv2:sku')")
    public CommonResult<Boolean> updateSkuById(@Validated @RequestBody SkuAttrJsonSimpleReqVO reqVO) {
        spuV2Service.updateSkuAttrJsonBySpuId(reqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出spu信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spuv2:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuExcel(@Valid SpuExportReqV2VO exportReqVO,
                               HttpServletResponse response) throws IOException {

        List<SpuDO> list = spuV2Service.getSpuList(exportReqVO);
        // 导出 Excel
        List<SpuExcelVO> datas = SpuConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "spu信息.xls", "数据", SpuExcelVO.class, datas);
    }
}
